package com.lzc.springboot_jsp_shiro.service.impl;

import com.lzc.springboot_jsp_shiro.dao.RoleDao;
import com.lzc.springboot_jsp_shiro.dao.UserDao;
import com.lzc.springboot_jsp_shiro.entity.Role;
import com.lzc.springboot_jsp_shiro.entity.User;
import com.lzc.springboot_jsp_shiro.service.UserService;
import com.lzc.springboot_jsp_shiro.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    /**
     * 注册
     *
     * @param user
     */
    @Override
    public void register(User user) {
        String salt = SaltUtils.getSalt(8);
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        userDao.save(user);
    }

    /**
     * 登录验证
     *
     * @param username
     * @return
     */
    @Override
    public User login(String username) {
        return userDao.findUserByUsername(username);
    }


}
